home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / rc-1.000 / rc-1 / rc-1.5-linux / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  2.6 KB  |  122 lines

  1. /* main.c: handles initialization of rc and command line options */
  2.  
  3. #include "rc.h"
  4.  
  5. bool dashdee, dashee, dashvee, dashex, dashell, dasheye,
  6.     dashen, dashpee, dashess, interactive;
  7. int rc_pid;
  8.  
  9. static bool dashoh;
  10.  
  11. static void assigndefault(char *,...);
  12. static void checkfd(int, enum redirtype);
  13.  
  14. extern void main(int argc, char *argv[], char *envp[]) {
  15.     char *dashsee[2], *dollarzero, *null[1];
  16.     int c;
  17.     initprint();
  18.     dashsee[0] = dashsee[1] = NULL;
  19.     dollarzero = argv[0];
  20.     rc_pid = getpid();
  21.     dashell = (*argv[0] == '-'); /* Unix tradition */
  22.     while ((c = rc_getopt(argc, argv, "nolpeivdxsc:")) != -1)
  23.         switch (c) {
  24.         case 'l':
  25.             dashell = TRUE;
  26.             break;
  27.         case 'e':
  28.             dashee = TRUE;
  29.             break;
  30.         case 'i':
  31.             dasheye = interactive = TRUE;
  32.             break;
  33.         case 'v':
  34.             dashvee = TRUE;
  35.             break;
  36.         case 'x':
  37.             dashex = TRUE;
  38.             break;
  39.         case 'd':
  40.             dashdee = TRUE;
  41.             break;
  42.         case 's':
  43.             dashess = dasheye = interactive = TRUE;
  44.             break;
  45.         case 'c':
  46.             dashsee[0] = rc_optarg;
  47.             goto quitopts;
  48.         case 'n':
  49.             dashen = TRUE;
  50.             break;
  51.         case 'p':
  52.             dashpee = TRUE;
  53.             break;
  54.         case 'o':
  55.             dashoh = TRUE;
  56.             break;
  57.         case '?':
  58.             exit(1);
  59.         }
  60. quitopts:
  61.     argv += rc_optind;
  62.     /* use isatty() iff -i is not set, and iff the input is not from a script or -c or -s flags */
  63.     if (!dasheye && !dashess && dashsee[0] == NULL && *argv == NULL)
  64.         interactive = isatty(0);
  65.     if (!dashoh) {
  66.         checkfd(0, rFrom);
  67.         checkfd(1, rCreate);
  68.         checkfd(2, rCreate);
  69.     }
  70.     initsignal();
  71.     inithash();
  72.     initparse();
  73.     assigndefault("prompt", "; ", "", (void *)0);
  74. #ifdef DEFAULTPATH
  75.     assigndefault("path", DEFAULTPATH, (void *)0);
  76. #endif
  77.     assigndefault("ifs", " ", "\t", "\n", (void *)0);
  78.     assigndefault("pid", nprint("%d", rc_pid), (void *)0);
  79.     initenv(envp);
  80.     initinput();
  81.     null[0] = NULL;
  82.     starassign(dollarzero, null, FALSE); /* assign $0 to $* */
  83.     inithandler();
  84.     if (dashsee[0] != NULL || dashess) {    /* input from  -c or -s? */
  85.         if (*argv != NULL)
  86.             starassign(dollarzero, argv, FALSE);
  87.         if (dashess)
  88.             pushfd(0);
  89.         else
  90.             pushstring(dashsee, TRUE);
  91.     } else if (*argv != NULL) {    /* else from a file? */
  92.         b_dot(--argv);
  93.         rc_exit(getstatus());
  94.     } else {            /* else stdin */
  95.         pushfd(0);
  96.     }
  97.     dasheye = FALSE;
  98.     doit(TRUE);
  99.     rc_exit(getstatus());
  100. }
  101.  
  102. static void assigndefault(char *name,...) {
  103.     va_list ap;
  104.     List *l;
  105.     char *v;
  106.     va_start(ap, name);
  107.     for (l = NULL; (v = va_arg(ap, char *)) != NULL;)
  108.         l = append(l, word(v, NULL));
  109.     varassign(name, l, FALSE);
  110.     if (streq(name, "path"))
  111.         alias(name, l, FALSE);
  112.     va_end(ap);
  113. }
  114.  
  115. /* open an fd on /dev/null if it is inherited closed */
  116.  
  117. static void checkfd(int fd, enum redirtype r) {
  118.     int new = rc_open("/dev/null", r);
  119.     if (new != fd && new != -1)
  120.         close(new);
  121. }
  122.